package com.fxcalibre.fxml.control;

import java.io.IOException;
import java.util.HashMap;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import com.fxcalibre.db.beans.Person;
import com.fxcalibre.fxml.control.events.DataChangedEvent;
import com.fxcalibre.fxml.view.FXMLViewLoader;
import com.fxcalibre.hibernate.HibernateUtils;
import com.fxcalibre.icons.ImageLoader;
import com.fxcalibre.syncservices.imdb.ImdbNameSearch;
import com.fxcalibre.syncservices.imdb.parser.NameResult;
import com.fxcalibre.utils.HTTPUtils;
import com.fxcalibre.utils.StringUtils;

public class PersonDetail extends VBox
{
	Stage containerStage;

	@FXML
	TextField id;
	@FXML
	TextField name;
	@FXML
	TextField imdbId;
	@FXML
	TextField bio;
	@FXML
	ImageView image;
	
	
	Person person;

	public PersonDetail()
	{
		FXMLViewLoader.load(this, "PersonDetail.fxml");
		repaint();
	}

	public void setPerson(Person person)
	{
		System.out.println("setPerson: " + person);
		this.person = person;
		repaint();
	}

	private void repaint()
	{
		if (person != null && person.getPersonId() != null)
		{
			id.setText(person.getPersonId() != null ? String.valueOf(person.getPersonId()) : "");
			name.setText(person.getName());
			imdbId.setText(person.getImdbId());
			bio.setText(person.getBio());
			image.setImage(ImageLoader.getPersonImage(person.getPersonId(),214).getImage());
		} else
		{
			id.setText("");
			name.setText("");
			imdbId.setText("");
			bio.setText("");
			image.setImage(ImageLoader.getPersonImage(0,214).getImage());
		}
	}

	@FXML
	protected void savePerson(ActionEvent event)
	{
		System.out.println("Saving person:" + person );
		if (person != null)
		{
			person.setName(name.getText());
			person.setImdbId(imdbId.getText());
			HibernateUtils.grabar(person);
			System.out.println("Persona guardada!");
			fireEvent(new DataChangedEvent());

			if (containerStage != null)
			{
				containerStage.close();
			}
		}
	}

	@FXML
	protected void deletePerson(ActionEvent event)
	{
		if (person != null && person.getPersonId() > 0)
		{
			HibernateUtils.borrar(person);
			fireEvent(new DataChangedEvent());
		}
	}

	@FXML
	protected void syncByImdbId(ActionEvent event)
	{
		if (StringUtils.isNotEmpty(imdbId.getText()))
		{
			HashMap<String, String> result = ImdbNameSearch.findById(imdbId.getText());
			System.out.println("Result: " + result);
			if (result.containsKey("name"))
			{
				name.setText(result.get("name"));
			}
		}
	}

	@FXML
	protected void searchImdbByName(ActionEvent event)
	{
		if (StringUtils.isNotEmpty(name.getText()))
		{
			System.out.println("abrimos buscador de personas");
			ImdbSearchNameResult imdbSearchNameResult = new ImdbSearchNameResult();
			imdbSearchNameResult.setName(name.getText());
			imdbSearchNameResult.showInStage();
			if(imdbSearchNameResult.getSelectedNameResult() != null)
			{
				NameResult nameResult = imdbSearchNameResult.getSelectedNameResult();
				person.setName(nameResult.getName());
				person.setImdbId(nameResult.getId());
				person.setBio(nameResult.getBio());
				//image.setImage(new Image(nameResult.getImgUrl()));
				try
				{
					HTTPUtils.downloadImage(nameResult.getImgUrl(), person.getPersonId() + ".jpg");
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				repaint();
			}
		}
	}
	
	public Stage getContainerStage()
	{
		return containerStage;
	}

	public void setContainerStage(Stage containerStage)
	{
		this.containerStage = containerStage;
	}

	public void showInStage()
	{
		Stage dialogStage = new Stage();
		dialogStage.setTitle("Edit Person");
		dialogStage.initModality(Modality.APPLICATION_MODAL);
		dialogStage.initOwner(containerStage);
		this.setContainerStage(dialogStage);
		Scene scene = new Scene(this);
		dialogStage.setScene(scene);
		dialogStage.showAndWait();
	}

}
